// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var router = require("../hash/pageRouter");
	var top = require("../top/top_main");
	var footer = require("../footer/footer_main");
	var commodityCategoryId;
	var storage = window.sessionStorage;
	var content;
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化

	var init = function() {
		if (storage.getItem("mIndex1") != null) {
			pageIndex = storage.getItem("mIndex1");
		}
		router.init();
		$("div.holder").css("display", "none")
		$("#content").removeClass("none");
		inquireCommodityCategoryPageList();
		inquireBulkstockLatestCollectionList();
		setInterval(function() {
			inquireBulkstockLatestCollectionList();
			if (pageIndex >= totalPageCount) {
				pageIndex = 1;
			} else {
				pageIndex++;
			}
		}, 20000);

		initEvent();
	};

	var initEvent = function() {

		$("#publicInquire").addClass("nav_active");

		//		添加跳转
		$("#bulkstockGather").unbind("click").click(function() {
			location.href = "../publicInquire/publicInquireMain.html";
		});

		$("#bulkstockCollection").unbind("click").click(function() {
			location.href = "../publicInquire/publicInquireCollection.html";
		});

		$("#bulkstockPledge").unbind("click").click(function() {
			base.openUrl("publicInquireMain.html#publicInquire/publicInquirePledge.html");
		});

		//自动补全
		$("#bulkstockCalculator").autocomplete(seajs.data.handler + "inquireBulkstockPageList.json", {
			minChars: 1,
			max: 10,
			width: 120,
			matchCase: false, //不区分大小写
			scroll: false,
			dataType: 'json',
			scrollHeight: 500,
			//此处为传递而外的参数
			extraParams: {
				pageSize: 50,
			},
			//需要把data转换成json数据格式                      
			parse: function(data) {

				return $.map(eval(data.datas), function(row) {
					return {
						data: row,
						value: row.bulkstockName, //此处无需把全部列列出来，只是两个关键列
						result: row.bulkstockName + "(" + row.specificationName + ")"
					}
				});
			},
			formatItem: function(data, i, total) {
				if ($("#bulkstockCalculator").hasClass('nonemorelist')) {
					$("#bulkstockCalculator").removeClass('nonemorelist');
					return;
				};
				return "<table><tr><td>" + data.bulkstockName + "(" + data.specificationName + ")" + "</td></tr></table>";
			},
			formatMatch: function(data, i, total) {
				return data.bulkstockName;
			},
			formatResult: function(data, value) {
				return data.bulkstockName;
			}
		}).result(function(event, data, formatted) { //回调
			$('#bulkstockName').val(data.categoryName);
			if ($("#bulkstockName").hasClass('nonemorelist')) {
				$("#bulkstockName").removeClass('nonemorelist');
			} else {
				$("#bulkstockName").addClass('nonemorelist');
			};
			$("#bulkstockCalculator").attr("bulkstockInfoId", data.bulkstockInfoId);
		});

		//计算按钮
		$("#calculateButton").unbind("click").click(function() {
			var bulkstock = $(this).parents().find(".bulkstockName").val();
			var specification = bulkstock.substring(bulkstock.indexOf("(") + 1, bulkstock.length - 1);
			var quantity = $(this).parents().find(".bulkstockQuantity").val();
			var bulkstockName = bulkstock.substring(0, bulkstock.indexOf("("));
			//			var bulkstockInfoId = $(this).parents().find(".bulkstockName").attr("bulkstockInfoId");
			inquireBulkstockGatherLatestList1(bulkstockName, specification, quantity);
		});

	};

	var vticker = function() {
		$("#bulkstocGatherListBody").parent().vTicker({
			speed: 700, //滚动速度，单位毫秒。
			pause: 4000, //暂停时间，就是滚动一条之后停留的时间，单位毫秒。                          
			showItems: 1, //显示内容的条数。                          
			mousePause: true, //鼠标移动到内容上是否暂停滚动，默认为true。                           
			height: 0, //滚动内容的高度。
			direction: 'up', //滚动的方向，默认为up向上，down则为向下滚动。                           
			animation: 'fade', //动画效果，默认是fade，淡出。    
			mousePause: true //鼠标移动到内容上是否暂停滚动，默认为true。
		});
	};

	//查询商品价格
	var inquireBulkstockGatherLatestList1 = function(bulkstockName, specification, quantity) {
		$("#priceResult").html("0.00");
		base.fnAjax({
			action: "inquireBulkstockGatherLatestList.json",
			async: false,
			nocode: true,
			oData: {
				specification: specification,
				bulkstockName: bulkstockName
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var price = data.datas[0].price * quantity;
					$("#priceResult").html(price.toFixed(2));
				} else {
					//                  base.alert(2,"信息获取失败");
				}
			}
		});
	};

	//大宗商品类别列表
	var inquireCommodityCategoryPageList = function() {
		base.fnAjax({
			action: "inquireBulkstockCategoryPageList.json",
			async: false,
			nocode: true,
			oData: {
				activeState: true
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#commodityCategoryListTpl").html();
					var html = juicer(tpl, data);
					$("#commodityCategoryListBody").html(html);
					$(".type").unbind("click").click(function() {
						$("#bulkstocGatherListBody").html("");
						pageIndex = 1;
						inquireBulkstockLatestCollectionList($(this).attr("commodityCategoryId"));
						$(".type").removeClass("type_active");
						$(this).addClass("type_active");
					});
				} else {}
			}
		});
	};

	//大宗商品行情表
	var inquireBulkstockLatestCollectionList = function(commodityCategoryId) {
		base.fnAjax({
			action: "inquireBulkstockLatestCollectionList.json",
			async: false,
			nocode: true,
			oData: {
				pageIndex: pageIndex,
				pageSize: pageSize,
				bulkstockCategoryId: commodityCategoryId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#bulkstocGatherListTpl").html();
					var html = juicer(tpl, data);
					$("#bulkstocGatherListBody").html(html);
					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl($("#changepage"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							storage.setItem("mIndex1", pageIndex);
							inquireBulkstockLatestCollectionList();
						});
					$("div.holder").jPages({
						containerID: "bulkstocGatherListBody",
						previous: "←",
						next: "→",
						perPage: 20,
						delay: 100
					});
					riseAndFallColor();
					//					添加监测跳转
					$(".publicMonitor").unbind("click").click(function() {
						var bulkstockname = $(this).parent().parent().attr("bulkstockname");
						var specification = $(this).parent().parent().attr("specification");
						var latestgatherId = $(this).parent().parent().attr("latestgatherid");
						var region = $(this).parent().parent().attr("region");
						location.href = "../publicInquire/publicInquireCollectionMonitor.html?bulkstockName=" + encodeURI(encodeURI(bulkstockname)) + "&specification=" + encodeURI(encodeURI(specification)) + "&region=" + encodeURI(encodeURI(region)) + "&latestgatherId=" + latestgatherId;
					});
				} else {}
			}
		});
	};

	//改变涨跌颜色
	var riseAndFallColor = function() {
		var riseAndFall = $(".riseAndFall");
		for (var i = 0, j = riseAndFall.length; i < j; i++) {
			if (riseAndFall[i].innerHTML.charAt(0) == "-") {
				$(riseAndFall[i]).css("color", "#33A55E");
			} else if (riseAndFall[i].innerHTML.charAt(0) == "+") {
				$(riseAndFall[i]).css("color", "#F1592A");
			} else {
			}
		}
	};

	return {
		init: init
	};
});